<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0,
           maximum-scale=1.0, minimum-scale=1.0"/>
    <title>angular服务</title>
    <script src="js/angular.js"></script>
</head>
<body>
<!-- 初始化中全部是以key=value来书写的 -->
<div ng-app="myApp">

    <div ng-controller="firstController">
        {{name}}
        {{ni}}
    </div>

</div>
</body>
<script>
    //定义服务的第一种方式;
    //    var app=angular.module("nihao",[], function ($provide) {
    //        $provide.provider('providerService01', function () {
    //            this.$get= function () {
    //                return {msg:"你想想你到底是怎么了"};
    //            }
    //        })
    //    });

    //定义服务的第二种方式;
    var app= angular.module("myApp",[]);
    app.config(function ($provider) {
        $provider.provider("providerService01", function () {
            this.$get = function () {
                return {msg: "你想想你到底是怎么了"};
            }
        });

        //内部可以定义多个服务哦,这个是内部定义的第二个服务;
        $provider.provider("providerService02", function () {
            this.$get = function () {
                var _name="";
                var service={};
                service.setName= function (name) {
                    _name=name;
                };

                service.getName= function () {
                    return _name;
                };
                return service;
            }
        });
    });

    //无论是第一种，还是第二种方式，注册的服务都需要在controller中回调;
    app.controller("firstController", ['$scope','providerService01','providerService02',
        function ($scope, providerService01,providerService02) {
            $scope.name = providerService01.msg;
//            console.log(providerService01.msg);
            providerService02.setName("listshi");
            $scope.ni=providerService02.getName();
//            console.log(providerService02.getName());
        }]);

</script>
</html>